package com.mayisoftware.mayioa.business.plan.impl.serviceImpl;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanCustom;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanDetailConfig;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConfigDetailService;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanCustomService;
import com.mayisoftware.mayioa.business.plan.impl.dao.PlanCustomMapper;
import com.mayisoftware.mayioa.common.impl.support.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlanCustomServiceImpl implements IPlanCustomService {

    @Autowired
    private PlanCustomMapper planCustomMapper;

    @Autowired
    private IPlanConfigDetailService planDetailService;
    /**
     * 根据条件查询计划评价列表
     *
     * @param planCustom 评价条件
     * @return
     */
    @Override
    public List<PlanCustom> selectPlanCusList(PlanCustom planCustom) {
        return planCustomMapper.selectPlanCusList(planCustom);
    }

    /**
     * 查询当前计划评价列表
     * @param planCfgId
     * @return
     */
    public List<PlanCustom> selectPlanCusList(Long planCfgId)
    {
        return planCustomMapper.selectPlanCusListByPlanId(planCfgId);
    }

    /**
     * 根据id查询计划评价
     *
     * @param planCusId 评价id
     * @return
     */
    @Override
    public PlanCustom selectPlanCusById(Long planCusId) {
        return planCustomMapper.selectPlanCusById(planCusId);
    }

    /**
     * 插入计划评价
     *
     * @param planCustom 计划评价
     * @return
     */
    @Override
    public int insertPlanCus(PlanCustom planCustom) {
        return planCustomMapper.insertPlanCus(planCustom);
    }

    /**
     * 更新计划评价
     *
     * @param planCustom 计划评价
     * @return
     */
    @Override
    public int updatePlanCus(PlanCustom planCustom) {
        planDetailService.updatePlanItemName(planCustom.getPlanCusId(), planCustom.getPlanCusName());
        return planCustomMapper.updatePlanCus(planCustom);
    }

    /**
     * 删除计划评价
     *
     * @param ids id：以,分割
     * @return
     */
    @Override
    public int deletePlanCusIds(String ids) {
        Long[] idArray = Convert.toLongArray(ids);
        List<Long> removeIds = new ArrayList<Long>();
        for(Long id : idArray){
            List<PlanDetailConfig> inPlan = planDetailService.selectPlanItemByCfgItemIdAndCfgItemType(2,id.intValue());
            //查询自定义id不在计划中
            if (inPlan.isEmpty()){
                removeIds.add(id);
            }
        }
        return planCustomMapper.deletePlanCusIds((Long[]) removeIds.toArray());
    }
}
